<?php

class Khoa extends MY_Admin
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();

		// Check if the logged user is an administrator
		//$this->access_library->check_access();
		// Load needed models, libraries, helpers and language files
		$this->load->module_model('admin', 'khoa_model', 'khoa');
		$this->load->module_language('admin', 'general');
		$this->load->module_language('admin', 'khoa');
		$this->user = $this->access->get_user();
		$this->load->helper('url'); 
		//You should autoload this one ;)
	}

	// Public methods
	public function index() 
	{
		$this->access->restrict('Admin.Khoa.Index');
		$data['ItemID'] = 'User';
		$data['title'] = 'Danh sách Khoa';
		$data['description'] = 'Danh sách Khoa';
		$this->access->restrict();
		$this->load->library('basic_library');
		$this->load->library('pagination');
		
		$config = array();
		$config['base_url'] = base_url().'admin/khoa/';
		$config['total_rows'] = $this->khoa->count_all_khoa();
		$config['per_page'] = 30;
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3))? $this->uri->segment(3):0;
		
		$data['phongban'] = $this->khoa->get_all_khoa($config['per_page'], $page);
		//$data['lops'] = $this->phongban->get_tenlop();
		$data['links'] = $this->pagination->create_links();		
		
		$this->_template['page']	= 'khoa/list';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function search()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Tìm kiếm thông tin';
		$data['description'] = 'Tìm kiếm thông tin';
		$data['search_keyword'] = trim($this->input->post('keyword', TRUE));
		if($data['search_keyword']!=='')
		{
			$data['result'] = $this->khoa->search_data($data['search_keyword']);
			
				$this->_template['page']	= 'khoa/result';
				$this->system_library->load($this->_template['page'], $data, TRUE);
			
			
		}
		else
		{
			redirect('admin/khoa', 'refresh');
		}
	}
	
	public function add()
	{
		$this->access->restrict('Admin.Khoa.Add');
		$data['ItemID'] = 'User';
		$data['title'] = 'Thêm mới Phòng ban';
		$data['description'] = 'Thêm mới Phòng ban';

		$this->load->helper('form');
		$data['giaovien'] = $this->khoa->get_giaovien();
		$userid = $this->user->id;
		$lang_working = load_language_working();
		
		//here is validation form
		$this->form_validation->set_rules('makhoa', 'lang:form_makhoa', 'required|callback_check_makhoa');
		$this->form_validation->set_rules('tenkhoa', 'lang:form_tenkhoa', 'required');

		$this->form_validation->set_error_delimiters('', '<br />');

		if ($this->form_validation->run() == TRUE)
		{		
			$this->khoa->add_khoa();
			$this->session->set_flashdata('message', lang('successfully_created'));	
			redirect('admin/khoa/', 'refresh');
		}	
		
		$this->_template['page']	= 'khoa/add';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function check_makhoa()
	{
        if($this->khoa->check_makhoa())
        {
            $this->form_validation->set_message('check_makhoa','MAPB đã tồn tại, vui lòng chọn lại');
            return false;
        }
        else
        {
            
            return true;
        }
    }
    
	
	public function delete($id)
	{
		$this->access->restrict('Admin.Khoa.Delete');
		$this->khoa->delete_khoa($id);
		$this->session->set_flashdata('message', lang('successfully_deleted'));
		redirect('admin/khoa', 'refresh');
	}

	//Edit product
	public function edit($id)
	{
		$this->access->restrict('Admin.Khoa.Edit');
		//metadata
		$data['ItemID']		 = 'User';
		$data['title']		 = "Sửa thông tin sinh viên"; 
		$data['description'] = "Sửa thông tin sinh viên"; 
		$data['info'] 	= $this->khoa->get_khoa_info($id);
		$data['giaovien'] = $this->khoa->get_giaovien();
		
		$this->form_validation->set_rules('makhoa', 'lang:form_makhoa', 'required');
		$this->form_validation->set_rules('tenkhoa', 'lang:form_tenkhoa', 'required');	
					
		$this->form_validation->set_error_delimiters('', '<br />');
		
		if($this->form_validation->run() == TRUE)
		{
			$this->khoa->edit_khoa($id);
			$this->session->set_flashdata('message', lang('successfully_edited'));	
			redirect('admin/khoa/', 'refresh');	
		}	
		$this->_template['page']	= 'khoa/edit';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function enable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->khoa->change_status_khoa($id,1);
		redirect('admin/khoa', 'refresh');
		
	}
	public function disable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->khoa->change_status_khoa($id,0);
		redirect('admin/khoa', 'refresh');
	}
}

/* End of file jobs.php */